import {mixOutputSource, OutputSource, OutputSourceConfig} from '@/types';
import {request} from 'umi';

/**
 * 获取所有输出源
 */
export const getOutputSources = (server: string) => {
  return request<OutputSource[]>(`${server}/output/source`);
};

/**
 * 获取所有输出源配置
 */
export const getOutputSourceConfigs = (server: string) => {
  return request<OutputSourceConfig[]>(
    `${server}/output/getOutputSourceConfigs`,
  );
};

/**
 * 更新输出源配置
 * @param data
 */
export const updateOutputSourceConfig = (
  server: string,
  data: OutputSourceConfig[],
) => {
  return request(`${server}/output/updateOutputSourceConfigs`, {
    method: 'post',
    data,
  });
};


/**
 * 更新输出源配置
 * @param data
 */
export const getOutputList = (
    server: string,outType:number
) => {
  return request(`${server}/api/peerEquipment/getDeviceByUserId?outType=${outType}`, {
    method: 'get',
  });
};


/**
 * 获取盒子输出配置
 * @param data
 */
export const getAppOutPut =async (
    server: string,
) => {
  try {
    const result = await  fetch(`http://${server}/app/output`,{
      method: 'get',
      headers:{
        Authorization:`Basic ${btoa('admin:admin')}`
      },
    })
    return await result.json()
  }catch (e) {
    return {success:false,error:e}
  }
};

/**
 * 设置盒子流重启
 * @param data
 */
export const reconnectOutPut =async (
    server: string,
) => {
  try {
    const result = await  fetch(`http://${server}/app/output/reconnect`,{
      method: 'get',
      headers:{
        Authorization:`Basic ${btoa('admin:admin')}`
      },
    })
    return await result.json()
  }catch (e) {
    return {success:false,error:e}
  }
};
/**
 * 设置盒子输出配置
 * @param data
 */
export const updateAppOutPut =async (
    server: string,
    data: mixOutputSource,
) => {
  try {
    const result = await fetch(`http://${server}/app/output`,{
      method: 'post',
      headers:{
        Authorization:`Basic ${btoa('admin:admin')}`
      },
      body:JSON.stringify(data)
    })
    return await result.json()
  }catch (e) {
    return {success:false,error:e}
  }

};
/**
 * 设置盒子输出配置
 * @param data
 */
export const appPreview =async (
    server: string,
    type: string = 'jpg',
) => {
  try {
    const result = await fetch(`http://${server}/stream/preview?type=${type}`,{
      method: 'get',
      headers:{
        Authorization:`Basic ${btoa('admin:admin')}`
      }
    })
    return await result.blob()
  }catch (e) {
    return null
  }

};

